'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: stu_code.py
* @Time: 2025/7/2
* @All Rights Reserve By Brtc
'''
def is_reg(name):
    with open('./reg.txt', 'r+', encoding='utf-8') as f:
        all_lines = f.readlines()       # 一次性把所有行读出来
        for line in all_lines:          # 一个一个遍历列表里的内容
            data_dic = dict(eval(line.replace('\n', '')))
            if data_dic.get('name') == name:
                return True
            else:
                continue
    return False


def input_name():
    err_count = 3  # 错误上限为3
    while err_count > 0:
        err_count = err_count - 1  # 错误一次 -1
        name = input('请输入用户名（4到12个字符）：')
        if len(name) <= 4 or len(name) > 12: # 限制用户名长度
            print('用户名长度为4-12个字符之间')
        elif is_reg(name):  # 如果已被注册就报错
            print(f'用户名{name}已存在')
        else:
            # print('name有效')
            return name # 符合要求的用户名
    return None # 如果没有符合要求就返回none，用于判断是否成功

def input_password():
    err_count = 3  # 错误上限为3
    while err_count > 0:
        err_count = err_count - 1
        password = input('请输入密码（8到20位之间）：')
        if len(password) <= 8 or len(password) > 20:
            print(f'密码长度需要再8-20之间')
        else:
            # print('pwd有效')
            return password
    return None

def input_cpassword(password:str):
    err_count = 3
    while err_count > 0:
        err_count = err_count - 1
        cpassword = input('请再输入一次')
        if cpassword != password:
            print(f'密码不一致，你还有{err_count}次机会')
        else:
            return True
    return False

if __name__ == '__main__':
    name = input_name()
    if name is None:
        print('输入用户名不符合要求')
        exit(0)
    password = input_password()
    if password is None:
        print('输入密码不符合要求')
        exit(0)
    if not input_cpassword(password) :
        print('输入的两次密码不一致')
        exit(0)
    else:
        with open('./reg.txt', 'a', encoding='utf-8') as f:
            reg_new = {'name': name, 'password': password}
            f.write(str(reg_new))
            f.write('\n')
            print(f'用户注{name}册成功')